package com.dragon.modules.yuegou.service.impl.merchant;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.busd.DistrictEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantSalesMonthEntity;
import com.dragon.modules.yuegou.enums.code.DistrictCode;
import com.dragon.modules.yuegou.mapper.merchant.MerchantSalesMonthMapper;
import com.dragon.modules.yuegou.model.merchant.MerchantSales;
import com.dragon.modules.yuegou.model.merchant.MerchantSalesMonth;
import com.dragon.modules.yuegou.query.busd.DistrictQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantSalesMonthQuery;
import com.dragon.modules.yuegou.service.busd.DistrictService;
import com.dragon.modules.yuegou.service.merchant.MerchantSalesMonthService;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商铺销售额按月 服务实现
 *
 * @author pbuilder generated
 * @date 2019-07-11 14:42:53
 * @version 2.0
 */
@Slf4j
@Service("merMerchantSalesMonthService")
public class MerchantSalesMonthServiceImpl
		extends ServiceImpl<MerchantSalesMonthMapper, MerchantSalesMonthEntity>
		implements MerchantSalesMonthService {

    @Autowired
	private DistrictService districtService;

	private static final String DATE_FORMAT = "yyyy-MM";
	@Override
	public MerchantSales countRentalSales(String busdId, LocalDateTime statisticalTime){
		Check.notNull(busdId, DistrictCode.DISTRICT_NOTNULL.getMessage());
		Check.notNull(statisticalTime, "统计时间不能为空");
		MerchantSales merchantSales = new MerchantSales();
		MerchantSalesMonthQuery monthQuery = new MerchantSalesMonthQuery();
		monthQuery.setBusdId(busdId);
		DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_FORMAT);
		DateTimeFormatter dfQuery = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String month = df.format(statisticalTime) + "-01";
		monthQuery.setMonth(LocalDate.parse(month, dfQuery));
		monthQuery.setDel(false);
		List<MerchantSalesMonthEntity> list = list(monthQuery.toQueryWrapper());
		if(!CollectionUtils.isEmpty(list)){
			for(MerchantSalesMonthEntity merchantSalesMonth : list){
				if(merchantSalesMonth.getRentalManageFee()!=null){
					LocalDateTime localDateTime = merchantSalesMonth.getStatisticalTime();
					int days = localDateTime.getDayOfMonth();
					merchantSales.setRental(merchantSalesMonth.getRentalManageFee().divide(new BigDecimal(days), 2, BigDecimal.ROUND_HALF_UP));
				}
			}
		}
		return merchantSales;
	}

	@Override
	public List<MerchantSalesMonth> findList(String busdId){
		List<MerchantSalesMonth> list = baseMapper.findList(busdId);
		if(!CollectionUtils.isEmpty(list)){
			for (MerchantSalesMonth merchantSalesMonth: list){
				DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_FORMAT);
				String month = df.format(merchantSalesMonth.getMonth());
				merchantSalesMonth.setMonthStr(month);
			}
		}
		return list;
	}

	private List<MerchantSales> findMonthBusdDate(String month) {
		LocalDate now = LocalDate.now();
		LocalDate before = com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(month) ? now.minusMonths(1) : now.minusMonths(Integer.parseInt(month));
		DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_FORMAT);
		String beforeMonth = before.format(df);
		String afterMonth = now.format(df);

		List<MerchantSales> salesList = baseMapper.findMonthBusdDate(beforeMonth, afterMonth);


		DistrictQuery query = new DistrictQuery();
		List<DistrictEntity> districts = districtService.list(query.toQueryWrapper());

		Map<String, String> districtMap = new HashMap<>();
		for (DistrictEntity district : districts) {
			districtMap.put(district.getId(), district.getBcName());
		}

		for (MerchantSales sales : salesList) {
			if (districtMap.containsKey(sales.getBusdId())) {
				sales.setBusdName(districtMap.get(sales.getBusdId()));
			} else {
				sales.setBusdName("");
			}
		}
		log.debug("查询出来的原始数据：{}", JSON.toJSONString(salesList));
		return salesList;
	}

}